package com.lumlord.common.shiro.util;

import java.util.Map;

import com.lumlord.common.shiro.config.JwtAuthFilter;
import com.lumlord.util.ServletUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;

import com.lumlord.common.domain.User;
import com.lumlord.common.shiro.service.DbShiroRealm;
import com.lumlord.util.BeanUtils;
import com.lumlord.util.StringUtils;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;


/**
 * shiro 工具类
 *	
 * @author gpf
 */
@Component
public class ShiroUtils
{	
    private static  final  ThreadLocal<String>  userToken = new ThreadLocal<String>();
	
    public static Subject getSubjct()
    {
        return SecurityUtils.getSubject();
    }

    public static Session getSession()
    {
        return SecurityUtils.getSubject().getSession();
    }

    public static void logout()
    {
        getSubjct().logout();
    }
    
    public static Map getUserMap()
    {
    	User user = getUser();
    	Map map = null;
    	if(null!=user) {
    		map = BeanUtil.beanToMap(user);
    		map.remove("password");
    	}
    	return map;
    }

    /**
     * 以token 注入用户为准
     * @param tooken
     * @return
     */
    public static User getUserByToken(String tooken){
        String account = satticJwtUtils.getAccount(tooken);
        userToken.set(tooken);
        return  getUser();
    }
    
    public static User getUser()
    {
    	User user = null;
        try {
        	Object obj = getSubjct().getPrincipal();
        	if (StringUtils.isNotNull(obj))
        	{
        		user = new User();
        		BeanUtils.copyBeanProp(user, obj);
        	}
        }catch (Exception e) {
        	
		}

        if(null!=user) {
        	return user;
        }else {
            if(null == userToken.get()){
                return  null;
            }
            String token =  userToken.get();
        	return  satticJwtUtils.getRedisUser(token);
        }
    }
    
    public static void setUser(User user)
    {
        userToken.set(user.getTooken());
        Subject subject = getSubjct();
        satticJwtUtils.setRedisUser(user.getTooken(),user);
        PrincipalCollection principalCollection = subject.getPrincipals();
        if(null == principalCollection) {
        	return;
        }
        String realmName = principalCollection.getRealmNames().iterator().next();
        PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(user, realmName);
        // 重新加载Principal
        subject.runAs(newPrincipalCollection);
    }

    public static void clearCachedAuthorizationInfo()
    {
        RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
        DbShiroRealm realm = (DbShiroRealm) rsm.getRealms().iterator().next();
        realm.clearCachedAuthorizationInfo();
    }

    public static Integer getUserId()
    {
        return null == getUser()?-1:getUser().getId();
    }

    public static String getLoginName()
    {
        return getUser().getAccount();
    }

    public static String getIp()
    {
        return getSubjct().getSession().getHost();
    }

    public static String getSessionId()
    {
        return String.valueOf(getSubjct().getSession().getId());
    }


    private static JwtUtils satticJwtUtils ;
    private static JwtAuthFilter satticJwtAuthFilter ;



    @Autowired
    private  JwtUtils jwtUtils ;



    @PostConstruct
    public void init() {
        satticJwtUtils = jwtUtils;
    }

}
